/**
 * 补充计划量
 * @author xiaoxujs
 */

import { Form, InputNumber, message, Modal } from "antd";
import { FC, useEffect } from "react";
import { IPlanPlanPage, IPlanSupplementPlanForm } from "../../../../../type/planManagement";
import { postPlanSupplementPlan } from "../../../api";

const SupplementPlanModule: FC<{
    open: boolean;
    clickData?: IPlanPlanPage;
    handleCallback: () => void;
}> = ({
    open,
    clickData,
    handleCallback
}) => {
    const [form] = Form.useForm<IPlanSupplementPlanForm>();
    const { validateFields, resetFields, setFieldsValue } = form;
    useEffect(() => {
        console.log(clickData)
      if(clickData) {
        setFieldsValue(clickData)
      }
    
      return () => {
        
      }
    }, [clickData, setFieldsValue])
    
    const handleOk = async () => {
        const values = await validateFields();
        if(clickData) {
            const params = {
                id: clickData.id,
                ...values
            }
            const { code, msg } = await postPlanSupplementPlan(params) as { code: number, msg: string };
            if (code === 20000) {
                message.success('修改成功');
                resetFields();
                handleCallback();
            } else {
                message.error(msg);
            }
        }
           

    };

    const handleCancel = () => {
        resetFields();
        handleCallback()
    };
    return <Modal title="添加计划" open={open} onOk={handleOk} onCancel={handleCancel}>
    <Form
        labelCol={{ span: 7 }}
        wrapperCol={{ span: 16 }}
        form={form}
    >
       
        <Form.Item<IPlanSupplementPlanForm>
            label="计划量（吨）"
            name="planQuantity"
            rules={[{ required: true, message: '请输入!' }]}
        >
            <InputNumber min={0}/>
        </Form.Item>
    </Form>
</Modal>    
}

export default SupplementPlanModule;